package com.pubinfo.passbook.eop.impl;

import com.alibaba.fastjson.JSONObject;
import com.pubinfo.passbook.common.entity.TContractBase;
import com.pubinfo.passbook.common.entity.TContractInvoiceApply;
import com.pubinfo.passbook.common.model.dto.contract.InvoiceCancelParam;
import com.pubinfo.passbook.common.model.eop.*;
import com.pubinfo.passbook.common.service.contract.TContractBaseService;
import com.pubinfo.passbook.common.service.contract.TContractInvoiceApplyService;
import com.pubinfo.passbook.eop.EopDubboService;
import com.pubinfo.uop.dubbo.api.UopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EopDubboServiceImpl implements EopDubboService {

    /**
     * 渠道编码
     */
    @Value("${eop.businessType}")
    private String businessType;

    /**
     * 渠道编码
     */
    @Value("${eop.channel}")
    private String channel;

    /**
     * 渠道秘钥
     */
    @Value("${eop.privateKey}")
    private String privateKey;

    /**
     * 系统编码
     */
    @Value("${eop.systemNbr}")
    private String systemNbr;

    /**
     * 受理工号
     */
    @Value("${eop.sysUserCode}")
    private String sysUserCode;

    @Resource
    private UopService uopService;

    @Resource
    private TContractBaseService contractBaseService;

    @Resource
    private TContractInvoiceApplyService contractInvoiceApplyService;

    @Override
    public JSONObject placeOrder(ApplyParam applyParam) {
    
        TContractBase contract = contractBaseService.getContractById(applyParam.getContractId());
    
        ApplyInvoiceHeadParam headParam = new ApplyInvoiceHeadParam(
            channel,
            privateKey,
            applyParam.getCheckPre() ? "orderPreValidate" : "ORDERPROCESSSTEP"
        );
    
        ApplyInvoiceBodyParam bodyParam = new ApplyInvoiceBodyParam();
        //业务类型
        bodyParam.setBusinessType(businessType);
        //流水号 和 真实单可以用一个
        bodyParam.setExtCustOrderId(applyParam.getInvoiceApplyId());
        //系统编码
        bodyParam.setSystemNbr(systemNbr);
        //受理工号
        bodyParam.setSysUserCode(sysUserCode);

        bodyParam.setQuickSimpleTradeDto(new QuickSimpleTradeDto() {{
            setCertType(1);
            setCertNum("330127198311132018");
        }});

        bodyParam.setQuickOtherInfoDto(new QuickOtherInfoDto() {{
            setPayModeCode(1000);//1000 前台收费
            setContactName(contract.getContractName());//合同名称
            setContactPhone(contract.getContractPhone());//合同手机号
            setRemark("");//合同备注
        }});
        List<QuickMainOfferOrderItemDto> quickMainOfferOrderItemDtoList = new ArrayList<>();
        List<QuickAccessProdOrderItemDto> quickAccessProdOrderItemDtos = new ArrayList<>();
        List<QuickChoiceOfferOrderItemDto> quickChoiceOfferOrderItemDtos = new ArrayList<>();

        //动作
        QuickMainOfferOrderItemDto quickMainOfferOrderItemDto = new QuickMainOfferOrderItemDto() {{
            //主套餐动作 3010100000-》新装 固定值
            setServiceOfferId("3010100000");
            //1200 后付费，1201准实时预付费，1201 准预付费实时信控，2100 预付费   固定值
            setPaymentModeCd(2100);
            //一次性费用 6219900633133001-》202005迁改赔补费X元 固定值
            setPromNbr("6219900633133001");

            setQuickAccessProdOrderItemDtos(quickAccessProdOrderItemDtos);
        }};
        quickMainOfferOrderItemDtoList.add(quickMainOfferOrderItemDto);

        QuickAccessProdOrderItemDto quickAccessProdOrderItemDto = new QuickAccessProdOrderItemDto() {{
            setProdNbr("PUB-MAN-0003");//一次性费用 PUB-MAN-0003 固定值
            setServiceOfferId("4010100000");//动作 新装 4010100000 固定值
            setQuickChoiceOfferOrderItemDtos(quickChoiceOfferOrderItemDtos);
        }};
        quickAccessProdOrderItemDtos.add(quickAccessProdOrderItemDto);

        //可选包
        quickChoiceOfferOrderItemDtos.add(new QuickChoiceOfferOrderItemDto(applyParam.getAmount()));

        bodyParam.setQuickMainOfferOrderItemDtoList(quickMainOfferOrderItemDtoList);

        log.info("headMap 11 -->, {}", JSONObject.toJSON(headParam));
        log.info("bodyJson  22 -->, {}", JSONObject.toJSON(bodyParam));
    
        Map<String, Object> result = uopService.uopServiceCall(
            JSONObject.parseObject(JSONObject.toJSONString(headParam)),
            JSONObject.parseObject(JSONObject.toJSONString(bodyParam)),
            new HashMap<>()
        );
        log.info("POST RESULT -->, {}", result.toString());
        System.out.println("111111111111111111");
    
        return new JSONObject(result);
    }
    //
    //@Override
    //public OrderResult orderDetails(OrderDetailsParam detailsParam) {
    //
    //    ApplyInvoiceHeadParam headParam = new ApplyInvoiceHeadParam(
    //        channel,
    //        privateKey,
    //        "QUERYPRODUCTORDER"
    //    );
    //
    //    OrderDetailsBodyParam bodyParam = new OrderDetailsBodyParam();
    //
    //    //业务类型
    //    bodyParam.setBusinessType(businessType);
    //    //系统编码
    //    bodyParam.setSystemNbr(systemNbr);
    //    //受理工号
    //    bodyParam.setSysUserCode(sysUserCode);
    //    bodyParam.setExtOrderNbr(detailsParam.getInvoiceApplyId());
    //    bodyParam.setScope(detailsParam.getScope());
    //    log.info("headMap 11 -->, {}", JSONObject.toJSON(headParam));
    //    log.info("bodyJson  22 -->, {}", JSONObject.toJSON(bodyParam));
    //    Map<String, Object> result = uopService.uopServiceCall(
    //        JSONObject.parseObject(JSONObject.toJSONString(headParam)),
    //        JSONObject.parseObject(JSONObject.toJSONString(bodyParam)),
    //        new HashMap<>()
    //    );
    //
    //    log.info("POST RESULT -->, {}", result);
    //    return JSON.parseObject(JSON.toJSONString(result), OrderResult.class);
    //}
    //
    
    /**
     * 统一平台取消订单
     */
    @Override
    public Map<String, Object> cancelOrder(InvoiceCancelParam param) {
        
        ApplyInvoiceHeadParam headParam = new ApplyInvoiceHeadParam(
            channel,
            privateKey,
            "CANCELPRODUCTORDER"
        );

        TContractInvoiceApply contractInvoiceApply = contractInvoiceApplyService.getById(param.getInvoiceId());

        JSONObject bodyParam = new JSONObject();
        //对端系统订单号
        bodyParam.put("extCustOrderNbr", contractInvoiceApply.getInvoiceApplyId());
        //取消原因
        bodyParam.put("cancelReason", param.getRemark());

        log.info("bodyJson -->, {}", bodyParam);

        Map<String, Object> result = uopService.uopServiceCall(
                JSONObject.parseObject(JSONObject.toJSONString(headParam)),
                bodyParam,
                new HashMap<>()
        );
        log.info("POST RESULT -->, {}", result.toString());

        return result;
    }

}
